package com.zytlifeservice.controller.find;


import com.zytlifeservice.domain.Result;
import com.zytlifeservice.domain.find.Commodity;
import com.zytlifeservice.domain.find.IntegralOrder;
import com.zytlifeservice.repository.find.CommodityRepository;
import com.zytlifeservice.service.UserService;
import com.zytlifeservice.service.find.CommodityService;
import com.zytlifeservice.service.find.IntegralOrderService;
import com.zytlifeservice.service.find.IntegralService;
import com.zytlifeservice.service.pay.PublicService;
import com.zytlifeservice.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 积分商品
 * Created by Administrator on 2017/6/1.
 */


@RestController
@RequestMapping(value = "/integral")
public class IntegralController {

    private final Logger logger = LoggerFactory.getLogger(IntegralController.class);

    @Autowired
    private CommodityService commodityService;//积分商品业务对象
    @Autowired
    private IntegralOrderService integralOrderService;//积分商品订单对象
    @Autowired
    private UserService userService;//用户对象
    @Autowired
    CommodityRepository commodityRepository;
    @Autowired
    PublicService publicService;

    @Autowired
    IntegralService integralService;

    //查看所有可兑换商品  返回所有的商品
    @RequestMapping(value = "/good")
    public Result findGoodList() {
        List<Commodity> goodList = commodityService.findAllCommodity();
        List<Map<String, Object>> goods = new ArrayList<Map<String, Object>>();
        if (null != goodList && goodList.size() > 0) {
            Map<String, Object> goodMap = null;
            for (Commodity commodity : goodList) {
                goodMap = new HashMap<String, Object>();
                goodMap.put("pid", commodity.getPid());
                goodMap.put("pexplain", commodity.getPexplain());
                goodMap.put("pcount", commodity.getPcount());
                goodMap.put("pname", commodity.getPname());
                goodMap.put("pimageUrl", commodity.getPimageUrl());
                goodMap.put("pneedIntegral", commodity.getPneedIntegral());
                goods.add(goodMap);
            }
            return ResultUtil.success(goods, "成功");
        } else {
            return ResultUtil.error(-1, "未获取到数据");
        }
    }

    //兑换商品生成订单  //获取积分的ok，下面是消费积分的过程。
    @RequestMapping(value = "/preCreate")
    public Result convertGood(Long userId, Long commodityId, Integer count) {
        //检查商品和账户积分
        Commodity commodity = commodityRepository.findOne(commodityId);
        int integral = count * commodity.getPneedIntegral();
        int isLoop = commodityService.changeGood(userId, commodityId, count, integral);
        int result = 0;//成功状态
        if (isLoop == 0) {
            IntegralOrder integralOrder = new IntegralOrder();
            integralOrder.setUserId(userId);
            integralOrder.setExpress("");
            integralOrder.setIntegral(integral);
            integralOrder.setCount(count);
            integralOrder.setCommodityId(commodityId);
            integralOrder.setCreateDate(new Date());
            integralOrder.setName(commodity.getPname());
            integralOrder.setTradeNo("");
            result = publicService.buyGood(integralOrder);
            if (result > 0) {
                result = integralOrderService.addIntegralOrder(integralOrder);//创建订单
                if (result > 0) {
                    return ResultUtil.success(null, "兑换成功");
                } else {
                    return ResultUtil.error(402, "兑换失败");
                }
            } else {
                return ResultUtil.error(402, "兑换失败");
            }
        } else if (isLoop == -1) {
            return ResultUtil.error(400, "商品数量不足");
        } else {
            return ResultUtil.error(401, "账户积分不足");
        }
    }

    //快递订单
    @RequestMapping(value = "/findUserOrder")
    public Result findUserOrder(Long userId) {
        System.out.println("进入查询用户订单方法");
        List<IntegralOrder> integralOrderList = integralOrderService.findUserAll(userId);
        if (null != integralOrderList && integralOrderList.size() > 0) {
            return ResultUtil.success(integralOrderList, "成功");
        } else {
            return ResultUtil.error(-1, "未获取到数据");
        }
    }

    @PostMapping(value = "/delete")
    public Result delete(long userId, long id) {
        integralOrderService.delete(userId, id);
        return ResultUtil.success();
    }

    @PostMapping(value = "/queryAll")
    public Result queryAllIntegral(int size, int page, long userId) {
        return ResultUtil.success(integralService.queryAllIntegral(page, size, userId), "查询成功");
    }

    @GetMapping(value = "/queryCustom")
    public Result queryCustom() {
        return ResultUtil.success(integralOrderService.findCustom(), "suc");
    }

}
